<template>
  <div class="main">
    <van-cell-group inset>
      <van-field
        v-model="username"
        required
        label="用户名"
        placeholder="请输入用户名" />
      <van-field
        v-model="password"
        required
        label="密码"
        placeholder="请输入密码" />
    </van-cell-group>
    <van-button size="small" type="primary" @click="login" class="login">登录</van-button>
  </div>
</template>
<script lang="ts" setup>
import { getLoginCode, getToken } from '@/api/login'
import { setToken, getToken as getSession } from '@/utils/auth'
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useStore } from '@/store'
const store = useStore()

const username = ref('')
const password = ref('')

const route = useRouter()
const login = async () => {
  const code = getLoginCode({
    name: username.value,
    pwd: password.value,
    clientId: '1583257773103439874'
  })
  const loginData = await getToken({
    code: (await code).data.code
  })

  setToken('token',loginData.token)
  // await store.dispatch('setUserInfo')
  store.commit('setUser', loginData)
  const roles = [{
			code: 0,
			name: '机构用户',
			to: '/index/total-preview'
		}, {
			code: 1,
			name: '企业用户',
			to: '/index/platform'
		}]
		const role: any = roles.find((item: any) => {
      return item.code == getSession('user').userType
    })
    route.push(role.to)
}
</script>
<style lang="scss" scoped>
.main{
  display: flex;
  align-items: center;
  justify-content: center;
  .login{
    margin-top: 20px;
  }
}
</style>